package com.bugreport.configuration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.unitils.UnitilsJUnit4;

import com.bugreport.configuration.BugTrackerConfigurationLoader;
import com.bugreport.utils.BugReportException;

public class BTCLLoadConfigurationIdsTestCase extends UnitilsJUnit4 {

	private static final Logger logger = Logger.getLogger(BTCLLoadConfigurationIdsTestCase.class);

	@Test
	public void loadConfigIds() throws BugReportException {

		// Given
		Properties properties = new Properties();
		properties.setProperty("1.type", "foo");
		properties.setProperty("1.foo", "bar");
		properties.setProperty("2.type", "foo2");
		properties.setProperty("3.type", "foo3");

		// When
		Set<Integer> configIds = BugTrackerConfigurationLoader.extractConfigurationIds(properties);

		// Then
		assertEquals(3, configIds.size());
	}

	@Test
	public void failToLoadIdWithOnlyOnePart() throws BugReportException {

		// Given
		Properties properties = new Properties();
		properties.setProperty("1", "foo");

		try {

			// When
			BugTrackerConfigurationLoader.extractConfigurationIds(properties);
			fail("Expected exception");

		} catch (BugReportException ex) {

			// Then
			assertNotNull(ex);
			assertEquals(100, ex.getCode());

			logger.debug(ex.getMessage());
		}
	}

	@Test
	public void failToLoadIdWithNonIntegerFirstPart() throws BugReportException {

		// Given
		Properties properties = new Properties();
		properties.setProperty("1t.ype", "foo");

		try {

			// When
			BugTrackerConfigurationLoader.extractConfigurationIds(properties);
			fail("Expected exception");

		} catch (BugReportException ex) {

			// Then
			assertNotNull(ex);
			assertEquals(101, ex.getCode());

			logger.debug(ex.getMessage());
		}
	}
}
